package cn.iocoder.yudao.module.biz.service.trainingPlan;

import cn.iocoder.yudao.framework.security.core.LoginUser;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.biz.controller.admin.trainingPlan.vo.*;
import cn.iocoder.yudao.module.biz.dal.dataobject.trainingPlan.BizTrainingPlanDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.biz.convert.trainingPlan.BizTrainingPlanConvert;
import cn.iocoder.yudao.module.biz.dal.mysql.trainingPlan.BizTrainingPlanMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.biz.enums.ErrorCodeConstants.*;

/**
 * 培训计划 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class BizTrainingPlanServiceImpl implements BizTrainingPlanService {

    @Resource
    private BizTrainingPlanMapper trainingPlanMapper;

    @Override
    public String createTrainingPlan(BizTrainingPlanCreateReqVO createReqVO) {
        // 设置单位
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        createReqVO.setDeptId(loginUser.getDeptId()+"");
        // 插入
        BizTrainingPlanDO trainingPlan = BizTrainingPlanConvert.INSTANCE.convert(createReqVO);
        trainingPlanMapper.insert(trainingPlan);
        // 返回
        return trainingPlan.getId();
    }

    @Override
    public void updateTrainingPlan(BizTrainingPlanUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateTrainingPlanExists(updateReqVO.getId());
        // 更新
        BizTrainingPlanDO updateObj = BizTrainingPlanConvert.INSTANCE.convert(updateReqVO);
        trainingPlanMapper.updateById(updateObj);
    }

    @Override
    public void deleteTrainingPlan(String id) {
        // 校验存在
        this.validateTrainingPlanExists(id);
        // 删除
        trainingPlanMapper.deleteById(id);
    }

    private void validateTrainingPlanExists(String id) {
        if (trainingPlanMapper.selectById(id) == null) {
            throw exception(TRAINING_PLAN_NOT_EXISTS);
        }
    }

    @Override
    public BizTrainingPlanDO getTrainingPlan(String id) {
        return trainingPlanMapper.selectById(id);
    }

    @Override
    public List<BizTrainingPlanDO> getTrainingPlanList(Collection<String> ids) {
        return trainingPlanMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<BizTrainingPlanDO> getTrainingPlanPage(BizTrainingPlanPageReqVO pageReqVO) {
        return trainingPlanMapper.selectPage(pageReqVO);
    }

    @Override
    public List<BizTrainingPlanDO> getTrainingPlanList(BizTrainingPlanExportReqVO exportReqVO) {
        return trainingPlanMapper.selectList(exportReqVO);
    }

}
